package plantshop.model.plants;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import plantshop.model.Characteristics;
import plantshop.model.Display;


public abstract class InvasivePlant extends Plant{

	private static final long serialVersionUID = 8351918936066329637L;
	private int spawnChance, spawns;
	private static Random rand = new Random();

	public InvasivePlant(String name, int seedPrice, int sellPrice, int value, Characteristics chars, int spawnChance) {
		super(name, seedPrice, sellPrice, value, chars);
		this.spawnChance = spawnChance;
		spawns = 0;
		invasive = true;
	}

	public boolean takeOver(){
		if(rand.nextInt(100) <= spawnChance/Math.pow(2, spawns)){
			spawns++;
			return true;
		}
		return false;
	}

	public List<Plant> getSurroundingNatives(Display disp){
		List<Plant> surrounding = new ArrayList<Plant>();
		int i = 0;
		Plant p = null;
		while(disp.getPlant(i) != this && i < disp.size()){
			i++;
		}
		if (i%2==0){
			p = disp.getPlant(i+1);
			if(p instanceof NativePlant)
				surrounding.add(p);
			if(i > 0){
				p = disp.getPlant(i-2);
				if(p instanceof NativePlant)
					surrounding.add(p);
			}
			if(i < disp.size()-1){
				p = disp.getPlant(i+2);
				if(p instanceof NativePlant)
					surrounding.add(p);
			}
		}else{
			p = disp.getPlant(i-1);
			if(p instanceof NativePlant)
				surrounding.add(p);
			if(i > 1){
				p = disp.getPlant(i-2);
				if(p instanceof NativePlant)
					surrounding.add(p);
			}
			if(i < disp.size()){
				p = disp.getPlant(i+2);
				if(p instanceof NativePlant)
					surrounding.add(p);
			}
		}
		return surrounding;
	}
	
	public void resetSpawns()
	{
		spawns = 0;
	}
}
